package com.yuantong.xyerp.core.provider.dao;

import com.yuantong.xyerp.core.api.dto.WarehouseReceiptListDto;
import com.yuantong.xyerp.core.api.model.WarehouseReceiptList;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Created by Jianguo.Yin on 2021-08-15.
 */
public interface WarehouseReceiptListMapper {

    /**
     * 库存单据的流水
     * @param warehouseReceiptId 某个库存单据
     * @param fuzzy
     * @param type 单据类型
     * @return
     */
    @Select({"<script> " +
            "SELECT wrl.*,wr.type,wr.total_weight,c.doc_no,c.contract_no FROM t_warehouse_receipt_list wrl " +
            "LEFT JOIN t_warehouse_receipt wr ON (wr.id = wrl.warehouse_receipt_id) " +
            "LEFT JOIN t_contract c ON (c.id = wr.business_id) " +
            "WHERE wrl.warehouse_receipt_id = #{warehouseReceiptId} " +
            "<if test=\"type != null and type != ''\"> " +
            "   AND wrl.type = #{type} " +
            "</if> " +
            "<if test=\"fuzzy != null and fuzzy != ''\"> " +
            "<bind name=\"bindKey\" value=\"'%'+fuzzy+'%'\"/> " +
            "   AND (c.doc_no LIKE #{bindKey} OR c.contract_no LIKE #{bindKey}) " +
            "</if> " +
            "<if test=\"ids != null and ids.size() > 0\"> " +
            "   AND wrl.id IN " +
            "<foreach collection=\"ids\" item=\"id\" open=\"(\" close=\")\" separator=\",\"> " +
            "#{id} " +
            "</foreach> " +
            "</if> " +
            "ORDER BY wrl.create_time DESC" +
            "</script>"})
    @ResultMap("WarehouseReceiptListDtoResult")
    List<WarehouseReceiptListDto> findAllByFuzzy(String warehouseReceiptId, String fuzzy, String type, List<String> ids);
}
